#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
from cx_Freeze import setup, Executable

# 确保目录存在
os.makedirs("build", exist_ok=True)

# 尝试手动清理构建目录
try:
    if os.path.exists("build/service"):
        # 尝试删除build/service目录
        print("尝试清理build/service目录...")
        try:
            # 使用shutil安全删除目录
            shutil.rmtree("build/service", ignore_errors=True)
        except Exception as e:
            print(f"清理目录时出错 (可以忽略): {e}")
except Exception:
    pass  # 忽略错误

# 依赖包
build_exe_options = {
    "packages": [
        "fastapi", 
        "uvicorn", 
        "uvicorn.logging",
        "uvicorn.lifespan",
        "uvicorn.protocols",
        "starlette", 
        "pydantic", 
        "typing", 
        "os", 
        "sys",
        "json", 
        "logging", 
        "asyncio", 
        "configparser", 
        "hashlib", 
        "datetime", 
        "win32serviceutil", 
        "win32service", 
        "win32event", 
        "servicemanager", 
        "socket", 
        "threading",
        "win32api",
        "win32print",
        "tempfile",
        "subprocess",
        "locale",
        "codecs",
        "uuid",
        "shutil",
        "time",
        "re",
        "importlib",
        "requests",
        "pathlib",
        "jinja2",
        "win32con",
        "platform",
        "collections",
        "queue",
        "ctypes",
        "email.mime",
        "email.utils",
        "http",
        "http.client",
        "http.cookies",
        "http.server",
        "xml.etree",
        "xml.dom",
        "email"
    ],
    "excludes": ["tkinter", "unittest", "pydoc"],
    "include_files": [
        ("static", "static"), 
        ("templates", "templates"), 
        ("README.md", "README.md"),
        ("config.ini", "config.ini")
    ],
    "include_msvcr": True,
    "includes": [
        "encodings", 
        "encodings.utf_8", 
        "uvicorn.logging", 
        "uvicorn.lifespan",
        "uvicorn.protocols",
        "uvicorn.main",
        "fastapi.middleware", 
        "fastapi.staticfiles",
        "fastapi.templating",
        "fastapi.responses",
        "http",
        "email",
        "xml"
    ],
    "zip_include_packages": "*",
    "zip_exclude_packages": [],
    "build_exe": "build/service"
}

# 目标文件
base = None
if sys.platform == "win32":
    base = "Win32Service"  # 对于服务使用

setup(
    name="云打印系统服务版",
    version="1.0.0",
    description="云打印系统服务版",
    options={"build_exe": build_exe_options},
    executables=[
        Executable(
            "app_service.py", 
            base=base,
            target_name="cloud_print_service_app.exe"
        )
    ]
) 